package main

import (
	"mangos/internal/logging"
	"os"
)

// initLog 初始化日志
func initLog() {
	// 获取日志核心
	core := logging.GetCore()

	// 获取日志目录
	logDir := os.Getenv("LOG_DIR")
	core.SetLogDir(logDir)

	// 初始化日志槽
	sink := &logging.DefaultSink{}
	sink.SetFormatter(&logging.TextFormatter{})
	sink.AddBackend(&logging.StderrBackend{})
	core.AddSink(sink)

	// 普通文件后端
	backend := &logging.FileBackend{
		FileName: "realmd.log",
		FileFlag: os.O_TRUNC,
	}
	backend.AddFilter(&logging.SeverityFilter{Severity: logging.Debug})
	backend.AddFilter(&logging.ChannelFilter{Channel: "log"})
	sink.AddBackend(backend)

	// 数据库
	backend = &logging.FileBackend{
		FileName: "realmd.db.log",
		FileFlag: os.O_TRUNC,
	}
	backend.AddFilter(&logging.SeverityFilter{Severity: logging.Debug})
	backend.AddFilter(&logging.ChannelFilter{Channel: "db"})
	sink.AddBackend(backend)
}

// logDebug 调试日志
func logDebug(format string, args ...any) {
	logging.Out(1, "log", logging.Debug, format, args...)
}

// log 输出日志
func log(format string, args ...any) {
	logging.Out(1, "log", logging.Info, format, args...)
}

// logError 输出错误日志
func logError(format string, args ...any) {
	logging.Out(1, "log", logging.Error, format, args...)
}

// logDB 输出数据库日志
func logDB(format string, args ...any) {
	logging.Out(1, "db", logging.Info, format, args...)
}

// logDBError 输出数据库错误日志
func logDBError(format string, args ...any) {
	logging.Out(1, "db", logging.Error, format, args...)
}
